package com.qf.ithub.movie_demo01.service.impl;

import com.alibaba.druid.sql.visitor.functions.Right;
import com.qf.ithub.common.exception.AppException;
import com.qf.ithub.common.result.ResponseEnum;
import com.qf.ithub.movie_demo01.entity.RoleRight;
import com.qf.ithub.movie_demo01.entity.RoleRightExample;
import com.qf.ithub.movie_demo01.entity.dto.GrantDTO;
import com.qf.ithub.movie_demo01.entity.dto.MenuDTO;
import com.qf.ithub.movie_demo01.mapper.RoleRightMapper;
import com.qf.ithub.movie_demo01.mapper.RrightMapper;
import com.qf.ithub.movie_demo01.service.RrightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author : xixi
 * @Title :
 * @date :  2021-03-18 20:38
 */
@Service
public class RrightServiceImpl implements RrightService {

    @Autowired
    private RrightMapper rrightMapper;

    @Autowired
    private RoleRightMapper roleRightMapper;

    @Override
    public List<MenuDTO> findMenuDTOByRoleid(Integer roleid) {
        List<MenuDTO> menuDTOS = rrightMapper.selectMenuByRoleidAndParentid(roleid, 0);
        //找出第一层的儿子
        if(menuDTOS==null || menuDTOS.size()==0){
            throw new AppException(ResponseEnum.ROLE_NO_MENUS);
        }
        for (MenuDTO menuDTO: menuDTOS) {
            //获得下层父ID
            Integer parengid = menuDTO.getRightid();
            List<MenuDTO> children = rrightMapper.selectMenuByRoleidAndParentid(roleid, parengid);
            menuDTO.setChirdren(children);
        }

        return menuDTOS;
    }

    /**
     *  根据角色id 获得该角色的动作权限码的集合
     */

    @Override
    public List<String> getActionCodesByRoleid(Integer roleid) {
        return roleRightMapper.selectRightCodesByRoleid(roleid);
    }

    @Override
    public List<MenuDTO> selectRightsByParentid() {
        //1、获得根上的节点
        List<MenuDTO> roots = rrightMapper.selectRightsByParentid(0);
        for (MenuDTO root:roots) {
            List<MenuDTO> menuDTOS = rrightMapper.selectRightsByParentid(root.getRightid());
            root.setChirdren(menuDTOS);
        }

        return roots;
    }

    @Override
    public List<Integer> selectRightidsByRoleNoParent(Integer roleid) {
        return rrightMapper.selectRightidsByRoleNoParent(roleid);
    }

    @Override
    @Transactional
    public void grantMenu(GrantDTO dto) {
        // 1、先删除 指定角色的所有权限
        RoleRightExample example = new RoleRightExample();
        example.createCriteria().andRoleidEqualTo(dto.getRoleid());
        roleRightMapper.deleteByExample(example);
        System.out.println(dto);
        // 2、然后再一条一条的插进去
        List<Integer> ids = dto.getRights();
        for (int i = 0; i<ids.size(); i++){
            RoleRight roleRight = new RoleRight();
            roleRight.setRightid(ids.get(i));
            roleRight.setRoleid(dto.getRoleid());
            roleRightMapper.insertSelective(roleRight);
        }

    }


}
